package com.mxl;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @description: 序列化测试
* @author mxl
* @date 2022-02-17 17:17
*/
@Slf4j
public class DogSerializableTest {

    /**
     * 写入文件
     *
     * @param dogList 狗集合
     */
    public static void writerFile(List<Dog> dogList) throws IOException {

        BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/java/com/mxl/dog.txt"));
        try {
            //遍历dog集合
            for(Dog dog : dogList){
                StringBuilder sb = new StringBuilder();
                sb.append(dog.getVariety()).append(",").append(dog.getHair()).append(",").append(dog.getColour()).append(",");
                bw.write(sb.toString());
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            log.error("写入异常" + e.getMessage());
        } finally {
            bw.close();
        }
    }
    public static void main(String[] args) throws IOException {
        ArrayList<Dog> list = new ArrayList<Dog>();
        list.add(new Dog("泰迪","短毛","金色"));
        list.add(new Dog("哈士奇","长毛","白色"));
        list.add(new Dog("柯基","短毛","黄色色"));
        writerFile(list);
    }

}
